//
//  AhaNaviUtils.m
//  jetour_line
//
//  Created by aha on 2023/6/19.
//
#import <MapKit/MapKit.h>
#import "AhaNaviUtils.h"

@implementation AhaNaviUtils
+ (void)goNavi:(NSDictionary*)dic{
    double lat = [[dic objectForKey:@"lat"] doubleValue];
    double lng = [[dic objectForKey:@"lng"] doubleValue];
    NSString *title = [dic objectForKey:@"title"];
    NSString *type = [dic objectForKey:@"type"];
    NSURL *mapUrl ;
    
    if([type isEqualToString:@"百度地图"]){
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%fd,%f|name=%@&mode=driving&coord_type=gcj02",lat,lng,title] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        mapUrl = [NSURL URLWithString:urlString];

    }else if([type isEqualToString:@"高德地图"]){
        
        mapUrl = [NSURL URLWithString:[[NSString stringWithFormat:@"iosamap://navi?sourceApplication=捷途路书&poiname=%@&poiid=BGVIS&lat=%f&lon=%f&dev=1&style=2",title,lat,lng] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                 
    }else if([type isEqualToString:@"腾讯地图"]){
        
        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&to=%@&tocoord=%f,%f",title,lat, lng] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        mapUrl = [NSURL URLWithString:urlString];

    }else if([type isEqualToString:@"苹果地图"]){
        [self appleMap:lat lng:lng andTitle:title];
        return;
    }
    if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) {
        //iOS10以后,使用新API
        [[UIApplication sharedApplication] openURL:mapUrl options:@{} completionHandler:^(BOOL success) {
            NSLog(@"scheme调用结束");
        }];
    } else {
        //iOS10以前,使用旧API
        [[UIApplication sharedApplication] openURL:mapUrl];
    }
}


//苹果原生地图
+ (void)appleMap:(double)lat lng:(double)lng andTitle:(NSString *)title{
    CLLocationCoordinate2D desCoordinate = CLLocationCoordinate2DMake(lat, lng);

    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    currentLocation.name = @"我的位置";
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:nil]];
    toLocation.name = [NSString stringWithFormat:@"%@",title];

    [MKMapItem openMapsWithItems:@[ toLocation]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeTransit,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}

 
@end
